package com.kzd.gtgame.main.money

import android.view.View
import androidx.lifecycle.LifecycleOwner
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.setOnClickAntiShakeListener
import com.kzd.gtgame.jetpack.vm.MoneyViewModel
import com.kzd.gtgame.main.ProblemDetailsAction
import com.kzd.gtgame.main.money.adapter.MoneyBossInviteAdapter
import com.kzd.gtgame.main.money.adapter.MoneyBossSharedAdapter
import com.kzd.gtgame.main.toProblemDetailsActivity
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_money_task.*
import kotlinx.android.synthetic.main.activity_money_task_boss.*

class MoneyTaskBossActivity : OwnerViewModeActivity<MoneyViewModel>() {

    private val mMoneyBossSharedAdapter = MoneyBossSharedAdapter()

    private val mMoneyBossInviteAdapter = MoneyBossInviteAdapter()

    override fun viewModel(): MoneyViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_money_task_boss

    override fun stateBar(): StatusBar {
        return StatusBar(color = R.color.colorMain, view = flMoneyTaskBossBar)
    }

    override fun initView() {
        rvMoneyTaskBossSharedBody.adapter = mMoneyBossSharedAdapter.apply {
            setOnItemChildClickListener { _, view, position ->
                val itemData = getItem(position)
                if (view.id == R.id.tvMoneyBossSharedSubmit) {
                    viewModel.userShare(
                        activity = this@MoneyTaskBossActivity,
                        url = itemData.share,
                        title = itemData.share_title,
                        desc = itemData.share_content
                    )
                }
            }
        }
        rvMoneyTaskBossInviteBody.adapter = mMoneyBossInviteAdapter


        tvMoneyTaskBossDetails.setOnClickAntiShakeListener {
            toProblemDetailsActivity(ProblemDetailsAction.AWARD)
        }
    }

    override fun initData() {
        viewModel.moneyBossShared()
        viewModel.moneyBossInvite()
    }

    override fun initViewMode() {
        viewModel.moneyBossSharedLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            if (data.isNullOrEmpty()) {
                rvMoneyTaskBossSharedBody.visibility = View.GONE
                rvMoneyTaskBossBackImage.visibility = View.VISIBLE
            } else {
                rvMoneyTaskBossSharedBody.visibility = View.VISIBLE
                rvMoneyTaskBossBackImage.visibility = View.GONE
                mMoneyBossSharedAdapter.setNewInstance(data)
            }
        })
        viewModel.moneyBossInviteLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            mMoneyBossInviteAdapter.setNewInstance(data)
        })
    }
}